﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Data.Tracking;

#endregion

namespace ScrumTable.DL.Data.Generic.Cache
{
    /// <summary>
    /// This class contains all cached data elements.  All data
    /// mutations of should be executed on the current class.
    /// </summary>
    public class CacheIdentityElementFactory : CacheElementFactory
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly CacheIdentityStore _identities;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a function pointer which is used to retrieve a unique id
        /// from a type and backend id.
        /// </summary>
        protected override Func<string, int, string> UniqueIdGetAction
        {
            get { return ResolveUnqiueId; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheElementFactory"/> class.
        /// </summary>
        public CacheIdentityElementFactory(CacheIdentityStore identityStore, IdGenerator idGenerator)
            : base(idGenerator)
        {
            PreCondition.AssertNotNull(identityStore, "identityStore");

            _identities = identityStore;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        private string ResolveUnqiueId(string category, int id)
        {
            // make sure the unique id's of the internal cache elements are reused in
            // order to retrieve the changed elements in cache
            if (id != CacheIdentity.DefaultId && _identities.ContainsKey(category, id))
            {
                return _identities[category, id].UniqueId;
            }
            return null;
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}